package com.zzqstart.cateservice.controller;


import com.zzqstart.cateservice.entity.CateRecipe;
import com.zzqstart.cateservice.entity.vo.BackRecipeQuery;
import com.zzqstart.cateservice.entity.vo.PublishRecipeVo;
import com.zzqstart.cateservice.service.CateRecipeCollectService;
import com.zzqstart.commonutils.JwtUtils;
import com.zzqstart.commonutils.entity.RecipeDetailFrontVo;
import com.zzqstart.cateservice.entity.vo.RecipeQueryVo;
import com.zzqstart.cateservice.service.CateRecipeService;
import com.zzqstart.commonutils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-03-06
 */
@Api(tags = "菜谱数据显示")
@RestController
@RequestMapping("/cateservice/caterecipe")
public class CateRecipeController {

    @Autowired
    private CateRecipeService cateRecipeService;

    @Autowired
    private CateRecipeCollectController cateRecipeCollectController;

    @ApiOperation(value = "根据菜谱ID获取菜谱详细")
    @GetMapping("/getRecipeDetailById/{recipeId}")
    public R getRecipeDetailById(@PathVariable String recipeId) {
        RecipeDetailFrontVo createRecipeVo = cateRecipeService.getRecipeDetailById(recipeId);
        return R.ok().data("recipe", createRecipeVo);
    }

    @ApiOperation(value = "根据菜谱ID获取菜谱发表信息")
    @GetMapping("/getRecipePublishInfoById/{recipeId}")
    public R getRecipePublishInfoById(@PathVariable("recipeId") String recipeId) {
        PublishRecipeVo recipe = cateRecipeService.getRecipePublishInfoById(recipeId);
        return R.ok().data("recipe", recipe);
    }

    @ApiOperation(value = "根据MemberID获取菜单")
    @PostMapping("/getRecipeListByMemberId/{page}/{limit}")
    public R getRecipeListByMemberId(@PathVariable Long page, @PathVariable Long limit,
                                     @RequestBody(required = false) RecipeQueryVo recipeQueryVo) {
        Map<String, Object> map = cateRecipeService.getRecipeListByMemberId(page, limit, recipeQueryVo);
        return R.ok().data(map);
    }

    @ApiOperation(value = "后台根据条件查询数据")
    @PostMapping("/getRecipeBySearchObj/{page}/{limit}")
    public R getRecipeBySearchObj(@PathVariable long page, @PathVariable long limit, @RequestBody(required = false) BackRecipeQuery cateRecipe) {
        Map<String, Object> map = cateRecipeService.getRecipeBySearchObj(page, limit, cateRecipe);
        return R.ok().data(map);
    }

    @ApiOperation(value = "逻辑删除菜谱")
    @DeleteMapping("/deleteRecipe/{recipeId}")
    public R deletRecipe(@PathVariable String recipeId) {
        boolean isdelete = cateRecipeService.removeById(recipeId);
        if (isdelete) {
            return R.ok();
        } else {
            return R.error().message("删除失败");
        }
    }

    @ApiOperation(value = "逻辑打回菜谱")
    @DeleteMapping("/returnRecipe/{recipeId}")
    public R returnRecipe(@PathVariable String recipeId) {
        CateRecipe recipe = new CateRecipe();
        recipe.setId(recipeId);
        recipe.setIsReturn(1);
        boolean isdelete = cateRecipeService.updateById(recipe);
        if (isdelete) {
            return R.ok();
        } else {
            return R.error().message("打回失败");
        }
    }

    @ApiOperation(value = "菜谱打分")
    @PostMapping("/markRecipe/{recipeId}/{grade}")
    public R markRecipe(@PathVariable String recipeId , @PathVariable BigDecimal grade, HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        boolean isGrade = cateRecipeService.markRecipe(recipeId, grade,memberId);
        if (isGrade){
            return R.ok();
        }else {
            return R.error().message("打分失败");
        }
    }

    @ApiOperation(value = "根据memberId获取收藏数据")
    @GetMapping("/getCollectRecipeByMemberId/{page}/{limit}")
    public R getCollectRecipeByMemberId(@PathVariable Long page, @PathVariable Long limit,HttpServletRequest request) {
        List<String> recipeIdList = cateRecipeCollectController.getRecipeIdListBymemberId(request);
        Map<String, Object> map = cateRecipeService.getCollectRecipeByMemberId(page, limit, recipeIdList);
        return R.ok().data(map);
    }

}

